package com.MTteam.activities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.MTteam.hdk.R;
import com.actionbarsherlock.app.SherlockActivity;
import com.tendcloud.tenddata.TCAgent;

public class FindClassroomAcitivity extends SherlockActivity implements OnItemSelectedListener {
	/////声明星期下拉框�?时间下拉框和查找按钮
	private static final String[] week={"星期一","星期二","星期三","星期四","星期五"};
	private static final String[] time={"整天","上午","下午","晚上","1-2节","3-4节","5-6节","7-8节"};
	private Spinner mWeek;
	private Spinner mTime;
	private Button mFind;
	private ProgressDialog mDialog;
	private ListView mResult;
	String rst="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.findclassroom);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle("课室查询");
		/////初始化控�?
		initView();
		/////将内容与adapter连接在一�?
		ArrayAdapter<String> weekadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,week);
		ArrayAdapter<String> timeadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,time);
		/////设置下拉列表的风�? 
        weekadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
        /////设置下拉列表的风�? 
        timeadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
        /////将adapter 添加到spinner�? 
        mWeek.setAdapter(weekadapter);
        mTime.setAdapter(timeadapter);
        /////添加事件Spinner事件监听    
        mWeek.setOnItemSelectedListener(this); 
        mTime.setOnItemSelectedListener(this); 
        /////添加按钮监听事件
        mFind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String selectedweek="time_";
				String selectedtime="";
				String selecteweek=mWeek.getSelectedItem().toString();
				String selectetime=mTime.getSelectedItem().toString();
				if("星期一".equals(selecteweek)){
					selectedweek+="1_";
				}else if("星期二".equals(selecteweek)){
					selectedweek+="2_";
				}else if("星期三".equals(selecteweek)){
					selectedweek+="3_";
				}else if("星期四".equals(selecteweek)){
					selectedweek+="4_";
				}else if("星期五".equals(selecteweek)){
					selectedweek+="5_";
				}
				if("整天".equals(selectetime)){
					selectedtime+="allday";
				}else if("上午".equals(selectetime)){
					selectedtime+="morning";
				}else if("下午".equals(selectetime)){
					selectedtime+="afternoon";
				}else if("晚上".equals(selectetime)){
					selectedtime+="night";
				}else if("1-2节".equals(selectetime)){
					selectedtime+="12";
				}else if("3-4节".equals(selectetime)){
					selectedtime+="34";
				}else if("5-6节".equals(selectetime)){
					selectedtime+="56";
				}else if("7-8节".equals(selectetime)){
					selectedtime+="78";
				}
				String[] params={selectedweek,selectedtime};
				new MyThread().execute(params);
			}
		});
	}
	/////初始化控�?
	private void initView(){
		mWeek=(Spinner) this.findViewById(R.id.sp_week);
		mTime=(Spinner) this.findViewById(R.id.sp_time);
		mFind=(Button) this.findViewById(R.id.bt_find);
		mDialog=new ProgressDialog(FindClassroomAcitivity.this);
		mDialog.setTitle("正在查询...");
		mDialog.setMessage("请稍等片刻");
		mResult=(ListView) this.findViewById(R.id.lv_show);
	}
	/////选中选项事件
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		//设置Spinner的字体大�?
        TextView tv=(TextView)view;
        tv.setTextSize(25);
        parent.setVisibility(View.VISIBLE);
		
	}
	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}
	
	/////获取查询结果线程
	private class MyThread extends AsyncTask<String, Integer, List<String>>{
		
		@Override
		protected void onPreExecute() {
			mDialog.show();
			super.onPreExecute();
		}
		@Override
		protected List<String> doInBackground(String... params) {
			
			List<String> classrooms=new ArrayList<String>();
			/////设置�?��传�?的参�?
			List<NameValuePair> pms =new ArrayList<NameValuePair>();
			pms.add(new BasicNameValuePair("week",params[0]));
			pms.add(new BasicNameValuePair("time",params[1]));
			/////使用post传输
			HttpPost post=new HttpPost("http://hdassistant.duapp.com/findclassrooms.php");
			try {
				post.setEntity(new UrlEncodedFormEntity(pms));
				post.setHeader("X-Online-Host", "http://hdassistant.duapp.com");
				HttpResponse response=new DefaultHttpClient().execute(post);
				if(response.getStatusLine().getStatusCode()==200){
					rst=EntityUtils.toString(response.getEntity())+params[0]+params[1];
					JSONArray jsons=new JSONArray(rst);
					for(int i=0;i<jsons.length();i++){
						JSONObject json=jsons.getJSONObject(i);
						classrooms.add(json.getString("location"));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return classrooms;
		}
		@Override
		protected void onPostExecute(List<String> result) {
			mDialog.dismiss();
			System.out.println(rst);
			mResult.setVisibility(View.VISIBLE);
			if(result!=null){
				mResult.setAdapter(new ArrayAdapter<String>(FindClassroomAcitivity.this, R.layout.findclassroom_item, result));
			}
			super.onPostExecute(result);
		}
	}
	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	protected void onResume() {
		super.onResume();
		TCAgent.onResume(this);
	};

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		TCAgent.onPause(this);
	}
}






